package game.structures;

import java.util.ArrayList;

import game.resources.Angle;
import game.resources.Collision;
import game.resources.ResourceManager;

public class Layer {
	
	private final int size = 128;
	private int[][] grid;
	private ArrayList<Tile> tileList;
	
	private Point gravityVector;
	
	public Layer() {
		grid = new int[][]
                 {  {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 0},
					{0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0,  0,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0,  0,  0,  3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0,  0,  0,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0, -1, -1,  2,  0,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0,  0,  0, -1,  1,  0,  0,  0, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0},
					{0,  0,  0, -1, -1, -1, -1,  0,  0, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1,  7, 0},
					{0,  0,  0,  8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  7,  0, 0},
					{0,  0,  0,  9, 10, -1, -1, -1, -1, -1, -1, -1, -1,  5,  6,  0,  3,  0,  0,  0, 0},
					{0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1, -1,  0, 0},
					{0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 0},
					{0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 0},
					{0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 0}
                 };
		tileList = new ArrayList<Tile>();
		gravityVector = new Point(0.0f, 1.0f);
	}
	
	public void init() {
		ResourceManager resourceManager = ResourceManager.getInstance();
		resourceManager.loadImages("src/assets/xml/testImages.xml");
		resourceManager.loadAnimation("src/assets/xml/tileTestAnimation.xml");

		tileList.add(new Tile(resourceManager.getAnimation("solidBlock"), 0));
		tileList.add(new Tile(resourceManager.getAnimation("halfBlockBottom"), 1));
		tileList.add(new Tile(resourceManager.getAnimation("halfBlockTop"), 2));
		tileList.add(new Tile(resourceManager.getAnimation("halfBlockLeft"), 3));
		tileList.add(new Tile(resourceManager.getAnimation("halfBlockRight"), 4));
		tileList.add(new Tile(resourceManager.getAnimation("deg30Slope1"), 5));
		tileList.add(new Tile(resourceManager.getAnimation("deg30Slope2"), 6));
		tileList.add(new Tile(resourceManager.getAnimation("deg45Slope"), 7));
		
		
		tileList.add(new Tile(resourceManager.getAnimation("quadIIIouter1"), 8));
		tileList.add(new Tile(resourceManager.getAnimation("quadIIIouter2"), 9));
		tileList.add(new Tile(resourceManager.getAnimation("quadIIIouter3"), 10));
		tileList.add(new Tile(resourceManager.getAnimation("quadIinner1"), 11));
		tileList.add(new Tile(resourceManager.getAnimation("quadIinner2"), 12));
		tileList.add(new Tile(resourceManager.getAnimation("quadIinner3"), 13));
	}
	
	public void update(int delta) {
		for (int x = 0; x < tileList.size(); x++) {
			tileList.get(x).update(delta);
		}
		
		
	}
	
	public void draw(Camera camera) {
		for	(int x = 0; x < grid.length; x++) {
			for (int y = 0; y < grid[0].length; y++) {
				if(grid[x][y] != -1)
					tileList.get(grid[x][y])
						.draw(	y * 128 - (int) camera.getCenter().getX(),
								x * 128 - (int) camera.getCenter().getY());
			}
		}
	}
	
	public boolean getCollision(Point point) {
		int id = getTileAt(point);
		if(id != -1)
			return Collision.isColliding(point, tileList.get(id).getID());
		return false;
	}
	
	public int getTileAt(Point point) {
		return grid[(int)point.getY()/size][(int)point.getX()/size];
	}
	
	//angle = current character angle
	//p = point at which new angle will be determined
	public float getAngle(float angle, Point point, Point direction) {
		int id = getTileAt(point);
		System.out.print(id + " ");
		//System.out.println(id);
		int width = 	(int) Math.floor(point.getX() / size);
		int height = 	(int) Math.floor(point.getY() / size);
		
		System.out.print(width + " " + height + " ");
		
		width = width * size + size / 2;
		height = height * size + size / 2;
		
		Point temp = new Point ( 	direction.getX() - width, 
									-(direction.getY() - height));
		
		return Angle.getTileAngle(id + 1, point, angle, temp);
	}
	
	public int getRight() { return grid[0].length * size; }
	public int getBottom() { return grid.length * size; }
	
}
